package com.carpedil.smvc.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carpedil.smvc.dao.*;
import com.carpedil.smvc.po.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/** @author Administrator */
@Component("userService")
public class UserServiceImpl implements UserService {
  @Qualifier("userMapper")
  @Autowired
  UserMapper userMapper;

  @Autowired UserDetailMapper udm;
  @Autowired UserRoleMapper urm;
  @Autowired RoleMapper roleMapper;
  @Autowired PermissionsMapper permissionsMapper;

  @Override
  @Transactional(rollbackFor = Exception.class)
  public boolean insertUser(User user) {
    UserDetail userDetail = user.getUserDetail();
    int insert = udm.insert(userDetail);
    if (insert > 0) {
      user.setFkUserDetailId(userDetail.getUserDetailId());
      insert = userMapper.insert(user);
    }
    return insert > 0;
  }

  @Override
  public boolean deleteUserById(Integer id) {
    return userMapper.deleteById(id) > 0;
  }

  @Override
  public boolean updateUser(User user) {
    return userMapper.updateById(user) > 0;
  }

  @Override
  public List<User> getUsersByCondition(Integer id, String name, String nickName) {
    QueryWrapper<User> wrapper = new QueryWrapper<>();
    wrapper
        .eq("id", id)
        .and(i -> i.like("user_name", name))
        .and(i -> i.like("nick_name", nickName));
    return userMapper.selectList(wrapper);
  }

  @Override
  public IPage<User> getUsersByPage(Integer pageNo, Integer pageSize) {
    return userMapper.selectPage(new Page<>(pageNo, pageSize), null);
  }

  @Override
  public IPage<User> getUsersByPageParam(Integer pageNo, Integer pageSize, Integer roleId) {
    Page<User> page = new Page<>(pageNo, pageSize);
    QueryWrapper<User> wrapper = new QueryWrapper<>();
    wrapper.eq("fk_role_id", roleId);
    return userMapper.getUsersByPageParam(page, wrapper);
  }

  @Override
  public User findUserById(Integer id) {
    User user = userMapper.selectById(id);
    UserDetail userDetail =
        udm.selectOne(
            new QueryWrapper<UserDetail>().eq("user_detail_id", user.getFkUserDetailId()));
    user.setUserDetail(userDetail);
    return user;
  }

  @Override
  public User login(User user) {
    User u =
        userMapper.login(
            new QueryWrapper<User>()
                .eq("user_name", user.getUserName())
                .eq("pass_word", user.getPassWord()));
    if (u != null) {
      return u;
    }
    return null;
  }

  @Override
  public User login2(User user) {
    User u = userMapper.login2(user.getUserName(), user.getPassWord());
    if (u != null) {
      List<Permissions> permissions = permissionsMapper.finPermissionsByUser(u.getUserName());
      u.setPermissions(permissions);
      return u;
    } else {
      return null;
    }
  }

  @Override
  public List<User> getAllUser() {
    List<User> list = userMapper.selectList(null);
    for (User user : list) {
      user.setUserDetail(
          udm.selectOne(
              new QueryWrapper<UserDetail>().eq("user_detail_id", user.getFkUserDetailId())));
      UserRole userRole =
          urm.selectOne(new QueryWrapper<UserRole>().eq("fk_user_id", user.getId()));
      if (userRole != null) {
        user.setRoles(
            roleMapper.selectList(new QueryWrapper<Role>().eq("pk_id", userRole.getFkRoleId())));
      }
    }
    return list;
  }

  @Override
  public List<User> findUserByRoleName(String roleName) {
    return userMapper.getUsersByWrapper(
        new QueryWrapper<User>().like("role_name", roleName.trim()));
  }
}
